<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>实时报警</title>
    <meta name="renderer" content="webkit" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1, maximum-scale=1"
    />
    <link
      rel="stylesheet"
      href="../lib/layui-v2.5.5/css/layui.css"
      media="all"
    />
    <link rel="stylesheet" href="../css/public.css" media="all" />
  </head>
  <style>
    td .layui-form-select {
      margin-top: -10px;
      margin-left: -15px;
      margin-right: -15px;
    }
  </style>
  <body>
    <div class="layuimini-container">
      <div class="layuimini-main">
        <div id="Myloading">
          <img
            src="../lib/layui-v2.5.5/css/modules/layer/default/loading-01.gif"
            alt=""
          />
        </div>
        <script type="text/html" id="toolbarDemo">
          <div class="layui-btn-container">
            <!-- <button class="layui-btn layui-btn-sm data-add-btn" lay-event="add"> 添加用户 </button> -->
            -->
            <!-- <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 配置权限 </button>  -->
          </div>
        </script>

        <table
          class="layui-hide"
          id="HcurrentTableId"
          lay-filter="currentTableFilter"
        ></table>

        <script type="text/html" id="HcurrentTableBar">
          {{# if(d.confirmFlag == 0){}}
          <a
            class="layui-btn layui-btn-xs data-count-edit layui-btn-danger"
            data-title="报警事项"
            lay-event="delete"
            >未确认</a
          >
          {{# }else{}}
          <a class="layui-btn layui-btn-xs" lay-event="edit">已确认</a>
          {{# } }}
        </script>
      </div>
    </div>
    <script src="../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
    <script src="../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
    <script>
      layui.use(["form", "table", "miniTab", "element"], function () {
        // 2020-03-25 HF 保存token
        var token = sessionStorage.getItem("newtoken");
        var ret_alarm = JSON.parse(sessionStorage.getItem("HpowerNumber"));

        var $ = layui.jquery,
          ele = layui.element,
          form = layui.form,
          miniTab = layui.miniTab,
          table = layui.table;
        miniTab.listen();
        var lastEventTime = "";
        var lastEventTime_time;

        //删除提醒标志
        $(window.parent.document).find("#msg_alarm").remove();

        //    首次获取数据
        getAlarm(lastEventTime);

        //   每八分钟获取一次数据
        setInterval(function () {
          getAlarm(lastEventTime_time);
        }, 480000);

        function getAlarm(lastEventTime) {
          $.ajax({
            url:
              "/oms-web/real-time/event-authority?token=" +
              token +
              "&lastEventTime=" +
              lastEventTime,
            type: "post",
            data: JSON.stringify(ret_alarm.stationAuthorityList),
            dataType: "json",
            contentType: "application/json",
            success: function (data) {
              if (data.code == -1) {
                window.location = "./login-1.html";
              }
              if (data.data == null || data.data == undefined) {
              } else {
                if (data.data.length != 0) {
                  lastEventTime_time = data.data[0].eventTime;
                  return getRetAlarm(data.data);
                } else {
                  return getRetAlarm(data.data);
                }
              }
            },
            error: function () {
              layer.msg("获取实时数据失败！");
            },
          });
        }

        function getRetAlarm(a) {
          table.render({
            elem: "#HcurrentTableId",
            toolbar: "#toolbarDemo",
            data: a,
            defaultToolbar: [
              "filter",
              "exports",
              "print",
              {
                title: "提示",
                layEvent: "LAYTABLE_TIPS",
                icon: "layui-icon-tips",
              },
            ],

            cols: [
              [
                //复选框 {type: "checkbox", width: 50, fixed: "left"},
                { type: "numbers", width: 60, title: "序号", align: "center" },
                {
                  field: "stationName",
                  width: 150,
                  title: "站名称",
                  align: "center",
                },
                {
                  field: "deviceName",
                  width: 150,
                  title: "设备名称",
                  align: "center",
                },
                {
                  field: "terminalName",
                  width: 130,
                  title: "终端名称",
                  align: "center",
                },
                {
                  field: "ptsName",
                  width: 100,
                  title: "测量点",
                  align: "center",
                },
                {
                  field: "dataValue",
                  width: 80,
                  title: "数值",
                  sort: true,
                  align: "center",
                },
                {
                  field: "typeText",
                  width: 130,
                  title: "报警类型",
                  align: "center",
                },
                {
                  field: "eventText",
                  minWidth: 50,
                  title: "报警描述",
                  align: "center",
                },
                {
                  field: "eventTime",
                  minWidth: 50,
                  title: "时间",
                  align: "center",
                },
                // {field: 'kj', minWidth: 50,title:'状态', fixed: "right", align: "center"}
                {
                  title: "操作",
                  width: 80,
                  templet: "#HcurrentTableBar",
                  fixed: "right",
                  align: "center",
                },
              ],
            ],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            done: function (res) {
              if (res.code == -1) {
                window.location = "./login-1.html";
              }
              // 表格渲染完成回调
              $("#Myloading").remove();
            },
          });
        }

        // 监听搜索操作
        form.on("submit(data-search-btn)", function (data) {
          // var result = JSON.stringify(data.field.username);
          // console.log(data);

          // layer.alert(result, {
          //     title: '最终的搜索信息'
          // });

          //执行搜索重载
          table.reload(
            "currentTableId",
            {
              //     page: {
              //         curr: 1
              //     }
              //     , where: {
              //         'text': result
              //     }
            },
            "data"
          );

          return false;
        });

        /**
         * toolbar监听事件
         */
        table.on("toolbar(currentTableFilter)", function (obj) {});

        //监听表格复选框选择
        table.on("checkbox(currentTableFilter)", function (obj) {});

        // 获取列表data
        table.on("tool(currentTableFilter)", function (obj) {
          if (obj.event === "edit") {
            layer.msg(
              "报警已确认，无须重复提交！",
              { icon: 2, time: 1300 },
              function () {}
            );
          } else if (obj.event === "delete") {
            var that = this;
            var str = obj.data;
            $.ajax({
              url:
                "/oms-web/real-time/event/confirm-authority?token=" +
                token +
                "&eventClass=" +
                str.eventClass +
                "&eventType=" +
                str.eventType +
                "&eventTime=" +
                str.eventTime +
                "&ptsCode=" +
                str.ptsCode,
              type: "post",
              // data:JSON.stringify([str.eventClass,str.eventType,str.eventTime,str.ptsCode]),
              dataType: "json",
              contentType: "application/json",
              success: function (data) {
                if (data.code == -1) {
                  window.location = "./login-1.html";
                }
                if (data.data == null || data.data == undefined) {
                } else {
                  $(that).replaceWith(
                    '<a class="layui-btn layui-btn-xs" lay-event="edit">已确认</a>'
                  );
                  layer.msg(
                    "操作成功",
                    { icon: 1, time: 1000 },
                    function () {}
                  );
                }
              },
              error: function () {
                layer.msg("获取实时数据失败！");
              },
            });
          } else if (obj.event === "set") {
          }
        });
      });
    </script>
  </body>
</html>
